package com.mulanweb.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PascalNameFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.TableMapping;

import java.util.List;

/**
 * Created by hcy on 2016/4/8.
 */
public class SupportController extends Controller
{

    protected int getPageNumber()
    {
        return super.getParaToInt("PageNumber");
    }

    protected int getPageSize()
    {
        return super.getParaToInt("PageSize");
    }

    protected void returnJson(int code, String message)
    {
        Ret ret = Ret.create("ReturnCode", code);
        if (StrKit.notBlank(message))
        {
            ret.put("AlertMessage", message);
        }
        super.renderJson(ret.getData());
    }

    protected Ret pageToRet(Page<?> page)
    {
        Ret ret = Ret.create("ReturnCode", 1);
        ret.put("TotalRow", page.getTotalRow());
        ret.put("TotalPage", page.getTotalPage());
        ret.put("PageNumber", page.getPageNumber());
        ret.put("PageSize", page.getPageSize());
        ret.put("DataList", page.getList());
        return ret;
    }

    protected void returnPageRecordJson(Page<Record> page)
    {
        page.getList().forEach(row -> {
            row.remove("tempcolumn");
            row.remove("temprownumber");
        });
        super.renderJson(JSON.toJSONString(pageToRet(page).getData(), new PascalNameFilter(), SerializerFeature.DisableCircularReferenceDetect));
    }

    protected void returnPageObjectJson(Page<Object> page)
    {
        super.renderJson(JSON.toJSONString(pageToRet(page).getData(), new PascalNameFilter(), SerializerFeature.DisableCircularReferenceDetect));
    }

    protected void returnPageModelJson(Page<? extends Model> page)
    {
        page.getList().forEach(row -> {
            row.remove("tempcolumn");
            row.remove("temprownumber");
        });
        super.renderJson(JSON.toJSONString(pageToRet(page).getData(), new PascalNameFilter(), SerializerFeature.DisableCircularReferenceDetect));
    }


    protected void returnFailureJson()
    {
        this.returnJson(0, "操作失败");
    }

    protected void returnSuccessJson()
    {
        this.returnJson(1, "操作成功");
    }

    protected void returnSuccessJson(Object object)
    {
        Ret ret = Ret.create("ReturnCode", 1);
        if (null != object)
        {
            ret.put("Data", object);
        } else
        {
            ret.put("Data", new Object());
        }
        super.renderJson(JSON.toJSONString(ret.getData(), new PascalNameFilter(), SerializerFeature.DisableCircularReferenceDetect));
    }

    protected void returnInvalidRequestJson()
    {
        this.returnJson(0, "无效的请求");
    }

    protected void returnStatusJson(boolean status)
    {
        if (status)
        {
            returnSuccessJson();
        } else
        {
            returnFailureJson();
        }
    }

    protected void returnSuccessJson(String message)
    {
        this.returnJson(1, message);
    }

    protected void returnFailureJson(String message)
    {
        this.returnJson(0, message);
    }
/*
    protected MAdminLogon getRequestAdminLogin()
    {
        String            auth    = getRequest().getHeader("Authorization");
        String            sql     = "SELECT * FROM " + TableMapping.me().getTable(MAdminLogon.class).getName() + " WHERE EncryptedToken = ?";
        List<MAdminLogon> records = MAdminLogon.dao.findByCache(BasicCacheNames.AdminLogon.FIND_BY_ENCRYPTED_TOKEN, auth, sql, auth);
        if (null != records && 0 < records.size())
        {
            return records.get(0);
        } else
        {
            return null;
        }
    }*/

}
